<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑职位</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
</head>
<body>
    <div th:replace="common/header :: header"></div>

    <div class="container mt-5">
        <div class="card shadow rounded-4">
            <div class="card-header bg-primary text-white rounded-top-4">
                <h5 class="mb-0"><i class="bi bi-pencil-square"></i> 编辑职位</h5>
            </div>
            <div class="card-body">
                <form id="jobForm" onsubmit="return submitForm(event)">
                    <input type="hidden" name="id" th:value="${job.id}">
                    
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">职位名称 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control rounded-pill" name="title" th:value="${job.title}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">部门 <span class="text-danger">*</span></label>
                            <select class="form-select rounded-pill" name="department" required>
                                <option value="">选择部门</option>
                                <option th:each="dept : ${departments}" 
                                        th:value="${dept}" 
                                        th:text="${dept}"
                                        th:selected="${dept == job.department}">
                                </option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">最低薪资(K) <span class="text-danger">*</span></label>
                            <input type="number" class="form-control rounded-pill" name="salaryMin" th:value="${job.salaryMin}" required min="0">
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">最高薪资(K) <span class="text-danger">*</span></label>
                            <input type="number" class="form-control rounded-pill" name="salaryMax" th:value="${job.salaryMax}" required min="0">
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">职位描述 <span class="text-danger">*</span></label>
                        <textarea class="form-control rounded-3" name="description" rows="4" required th:text="${job.description}"></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">任职要求 <span class="text-danger">*</span></label>
                        <textarea class="form-control rounded-3" name="requirements" rows="4" required th:text="${job.requirements}"></textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">状态</label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="status" value="OPEN" th:checked="${job.status == 'OPEN'}">
                            <label class="form-check-label">招聘中</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="status" value="CLOSED" th:checked="${job.status == 'CLOSED'}">
                            <label class="form-check-label">已结束</label>
                        </div>
                    </div>

                    <div class="text-center">
                        <button type="submit" class="btn btn-success rounded-pill px-4 me-2">
                            <i class="bi bi-save"></i> 保存
                        </button>
                        <button type="button" class="btn btn-outline-secondary rounded-pill px-4" onclick="history.back()">
                            <i class="bi bi-x"></i> 取消
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>
        function submitForm(event) {
            event.preventDefault();
            
            const form = document.getElementById('jobForm');
            const formData = new FormData(form);
            const data = Object.fromEntries(formData.entries());
            
            // 验证薪资范围
            if (parseInt(data.salaryMin) > parseInt(data.salaryMax)) {
                alert('最低薪资不能大于最高薪资');
                return false;
            }

            // 确保所有必填字段都有值
            if (!data.title || !data.department || !data.description || !data.requirements) {
                alert('请填写所有必填字段');
                return false;
            }

            // 转换数值类型
            data.salaryMin = parseInt(data.salaryMin);
            data.salaryMax = parseInt(data.salaryMax);
            data.status = data.status || "OPEN"; // 默认为"OPEN"（招聘中）

            console.log('提交的数据：', data); // 添加调试日志

            axios.post('/job/edit/' + data.id, data)
                .then(response => {
                    if (response.data === 'success') {
                        alert('保存成功');
                        location.href = '/job/list';
                    } else {
                        alert('保存失败');
                    }
                })
                .catch(error => {
                    console.error('错误详情：', error); // 添加错误日志
                    alert('保存失败：' + (error.response?.data?.message || '未知错误'));
                });

            return false;
        }
    </script>
</body>
</html> 